package com.khalil.kkadmin.module.logging;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/logging")
public class LoggingMonitorController {

    private final LoggingMonitorService loggingMonitorService;

    public LoggingMonitorController(LoggingMonitorService loggingMonitorService) {
        this.loggingMonitorService = loggingMonitorService;
    }

    @GetMapping("/testError")
    public void testError() {
        try {
            throw new RuntimeException("test error");
        } catch (Exception e) {
            log.error("test error", e);
        }
    }

    @GetMapping("/getLoggingList")
    public List<LoggingError> getLoggingList(@RequestParam("startTime") String startTime,
                                             @RequestParam("endTime") String endTime) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long startTimeStamp = simpleDateFormat.parse(startTime).getTime();
        long endTimeStamp = simpleDateFormat.parse(endTime).getTime();

        return loggingMonitorService.getLoggingList(startTimeStamp, endTimeStamp);
    }

}
